package Leetcode.ArrayAndLinkedList.swapPairs24;

/**
 * 递归求解
 * 第一步 终止条件
 * 第二步 返回值
 * 第三步 每层该做的事情
 *
 */
public class Solution2 {
    public ListNode swapPairs(ListNode head) {
        if(head==null){
            return null;
        }
        if (head.next == null) {
            return head;
        }
        head = changeHead(head);
        if (head.next.next == null) {
            return head;
        }
        head.next.next = swapPairs(head.next.next);
        return head;
    }

    private ListNode changeHead(ListNode head) {
        ListNode p = head.next;
        head.next = p.next;
        p.next = head;
        return p;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};
        ListNode node = new ListNode(nums);
        System.out.println(node);
        Solution2 solution = new Solution2();
        System.out.println(solution.swapPairs(node));
    }
}
